package com.starbaba.accountTool.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.starbaba.accountTool.model.AccountBillRecord;
import com.starbaba.accountTool.service.AccountBillService;
import com.starbaba.base.dao.LoanManagerBaseDao;
import com.tuanzi.server.loan_base.util.DateUtils;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

public class AccountBillServiceImpl extends LoanManagerBaseDao implements AccountBillService{

	@Override
	public List<AccountBillRecord> getList(Map<String, Object> paramsMap,
			int pageNo, int pageSize) {
		
		String sql = generateGetListSql(paramsMap) + " ORDER BY ctime DESC LIMIT ?,? ";
		
		List<AccountBillRecord> list = new ArrayList<AccountBillRecord>();
		try {
			list = getDao().list(sql, AccountBillRecord.class, pageNo, pageSize);
			for(AccountBillRecord record: list){
				if(record.getInterest_rate_type().equals("d")){
					record.setInterest_rate(record.getInterest_rate() + "%(日)");
				}else if(record.getInterest_rate_type().equals("m")){
					record.setInterest_rate(record.getInterest_rate() + "%(月)");
				}
				record.setAccount(PhoneNumNideUtil.hideNum(record.getAccount()));;
			}
		} catch (AppException e) {
			
			LogService.error(e);
			
		}
		
		return list;
	}

	@Override
	public int countList(Map<String, Object> paramsMap) {
		
		String sql = "SELECT COUNT(*) FROM (" + generateGetListSql(paramsMap) + ") AS a";
		int result = 0;
		try {
			
			result = getDao().getInt(sql);
			
		} catch (AppException e) {
			
			LogService.error(e);
			
		}
		return result;
	}
	
	
	/**
	 * 生成查询语句sql
	 * @param paramsMap
	 * @return
	 */
	private String generateGetListSql(Map<String, Object> paramsMap){
		
		String startDate = (String) paramsMap.get("startDate");
		String endDate = (String) paramsMap.get("endDate");
		String account = (String) paramsMap.get("account");
		String loanName = (String) paramsMap.get("loanName");
		String periods = (String) paramsMap.get("periods");
		String startAmount = (String) paramsMap.get("startAmount");
		String endAmount = (String) paramsMap.get("endAmount");
		
		
		StringBuilder sql = new StringBuilder("SELECT * FROM `account_bill_record` WHERE 1 = 1");
		
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND ctime >= '" + DateUtils.getDateStartTime(startDate) + "'");
		}
		
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND ctime <= '" + DateUtils.getDateEndTime(endDate) + "'");
		}
		
		if(StringUtils.isNotEmpty(account)){
			sql.append(" AND account = '" + account + "'");
		}
		
		if(StringUtils.isNotEmpty(loanName)){
			sql.append(" AND party_a_name = '" + loanName + "'");
		}
		
		if(StringUtils.isNotEmpty(periods)){
			sql.append(" AND periods = '" + periods + "'");
		}
		
		if(StringUtils.isNotEmpty(startAmount)){
			sql.append(" AND amount >= '" + startAmount + "'");
		}
		
		if(StringUtils.isNotEmpty(endAmount)){
			sql.append(" AND amount <= '" + endAmount + "'");
		}
		
		return sql.toString();
	}

}
